<?php
require "../common/user_online.php";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>EbookAZ</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<script type="text/javascript" src="../scripts/rating.js"></script>
	<?php require "../components/head.php" ?>
</head>
<body onload="MakeCurrentPage(1)">
<div id="container">
	<?php require "../components/admin_menu.php" ?>
	<?php require "../components/admin_header.php" ?>
	<div id="content">
		<?php require "../components/content_left.php"?>
		<div id="content_right">
        <?php
		$userId = $_SESSION['user_id'];
		include "../common/db.inc";
		
		$mysqli = new mysqli($host, $username, $password,$dbname);
		$mysqli->set_charset('utf8');	

		/* check connection */
		if (mysqli_connect_errno()) {
		    printf("Connect failed: %s\n", mysqli_connect_error());
		    exit();
		}

		$id = $_REQUEST["id"];	
		$query = ("call sp_GetEbookById('$id')");

		if ($result = $mysqli->query($query)) 
		{		
			//lay thong tin ebook
			$row = mysqli_fetch_row($result);
			$ebookId = $row[0];
			$title = $row[1];
			$file_up = $row[2];
			$author = $row[5];
			$publisher = $row[6];
			$type = $row[3];
			$field = $row[11];
			$category = $row[10];
			$pagenum = $row[7];
			$size = $row[9];
			$mana = $row[14];
			$rating = $row[15];
			$downloadcount = $row[12];
			$updatedate = $row[13];
			$description = $row[4];
			$coverimg = "$image$row[8]";
			$file_up = $row[2];
			
			$a = floor($rating);
			$b = $rating - $a;
			
			$arr = date_parse($updatedate);
			$ngay = $arr['day'];
			$thang = $arr['month'];
			$nam = $arr['year'];
			if ($ngay < 10)
				$ngay = "0" . $ngay;
			if ($thang < 10)
				$thang = "0" . $thang;
			$updatedate = $ngay . "/" . $thang . "/" . $nam;
			
			//lay so diem danh gia cua user(neu co)
			$point = 0;
			$mysqli = new mysqli($host, $username, $password,$dbname);
			$query = "call sp_CheckUserRating('$userId', '$ebookId')";
			$result = $mysqli->query($query);
			if(mysqli_num_rows($result) != 0)
			{
				$row = mysqli_fetch_row($result);
				$point = $row[0];
			}
			
			//lay so phieu danh gia
			$vote = 0;
			$mysqli = new mysqli($host, $username, $password,$dbname);
			$query = "call sp_GetVote('$ebookId')";
			$result = $mysqli->query($query);
			if(mysqli_num_rows($result) != 0)
			{
				$row = mysqli_fetch_row($result);
				$vote = $row[0];
			}
			//lay loai file
			$lastpos = strrpos($file_up, '.');
			$filetype = substr($file_up, $lastpos + 1, strlen($file_up));
			
			//kiem tra ebook nay co trong danh sach can download chua
			$mysqli = new mysqli($host, $username, $password,$dbname);
			$query = "call sp_GetNeededBookByUser('$userId', '$ebookId')";
			$result = $mysqli->query($query);
			$isNeeded =  mysqli_num_rows($result);
			
			echo "<h1 align='center'> Chi tiết sách</h1>";
			echo "<table>";
			echo "<tr>";
			echo "<td valign='top' align='center' width='285px'><img width='240' height='350' src='$coverimg' alt='book image' /></td>";
			echo "<td valign='top'>";
			echo "<label style='color: blue; font-size:13px;'><b>$title</b></label><hr/><br/>";
			echo "<b>Tác giả: </b>$author<br/>";
			echo "<b>NXB: </b>$publisher<br/>";
			echo "<b>Loại ebook: </b>$type<br/>";
			echo "<b>Loại file: </b>$filetype<br/>";
			echo "<b>Danh mục: </b>$field >> $category<br/>";
			echo "<b>Số trang: </b>$pagenum<br/>";
			echo "<b>Kích thước: </b>$size<label> Kb</label><br/>";
			echo "<b>Mana: </b>$mana<br/>";
			echo "<b>Ngày cập nhật: </b>$updatedate<br/>";			
			
			echo "<div id='ratingstar'>";
			echo "<b class='left'>Đánh giá: </b>";
			echo "<div id='star'>";
			for($j=0; $j<$a; $j++)
				echo "<img src='../images/star_on.png' />";
			for($j=0; $j<$b; $j++)
				echo "<img src='../images/star_half.png' />";
			for($j=0; $j<5-ceil($rating); $j++)
				echo "<img src='../images/star_off.png' />";
			echo "</div>";
			echo "<label>&nbsp;&nbsp;($vote phiếu)</label>";
			echo "</div>";
			echo "<br/>";
			echo "<input type='button' value='Đánh giá ebook này' onClick='showRating($point)'></input><br/><br/>";
			echo "<div id='rateMe'>";
			echo "<a onclick='rateIt(1)' id='_1' title='Ehh...' onmouseover='rating(1)' onmouseout='off(1)'></a>";
			echo "<a onclick='rateIt(2)' id='_2' title='Not Bad' onmouseover='rating(2)' onmouseout='off(2)'></a>";
			echo "<a onclick='rateIt(3)' id='_3' title='Good' onmouseover='rating(3)' onmouseout='off(3)'></a>";
			echo "<a onclick='rateIt(4)' id='_4' title='Very good' onmouseover='rating(4)' onmouseout='off(4)'></a>";
			echo "<a onclick='rateIt(5)' id='_5' title='Excellent' onmouseover='rating(5)' onmouseout='off(5)'></a>";
			echo "</div>";
			echo "<b>Tải về: </b>$downloadcount<label> lần</label>&nbsp;&nbsp;&nbsp;";
			echo "<input type='button' value='Tải về' onClick='admin_download(\"$userId\", \"$ebookId\",\"$file_up\")'></input>&nbsp;";				
			echo "<br/><br/><a href='ebook_edit.php?id=$ebookId'>Cập nhật thông tin ebook</a>";

			echo "</td>";
			
			echo "</tr >";			
			echo "</table><br/>";
			
			echo "<div style='padding-left: 25px'>";
			echo "<label style='color: blue; font-size:13px;'><b>Mô tả</b></label><br/>";
			echo "$description";
			echo "</div>";
			echo "<input type='hidden' id='user' value='$userId'></input>";
			echo "<input type='hidden' id='ebook' value='$ebookId'></input>";
		}
		$mysqli->close();		

		?>
		</div>
	</div>
	<?php require "../components/footer.php" ?>
</div>
</body>
</html>
